# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=wt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.10.0
pkgrel=1
pkgdesc="A C++ library and application server for developing and deploying web applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="http://www.webtoolkit.eu/"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-glew"
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-pygments"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-tools"))
optdepends=("${MINGW_PACKAGE_PREFIX}-postgresql: for PostgreSQL Dbo support"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: for MySQL Dbo support"
            "${MINGW_PACKAGE_PREFIX}-sqlite3: for Sqlite Dbo support")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/emweb/wt/archive/${pkgver}.tar.gz"
         '001-wt-4.4.0-fix-find-gm.patch'
         '002-wt-4.4.0-skip-some-examples.patch'
         '003-wt-4.4.0-disable-win32-configdir.patch'
         '004-wt-4.4.0-disable-win32-examples-bat.patch')
sha512sums=('156ac130177f46151f8da8a8ce36714a7fd30713ac796c94867756309f7099f429e2e6e1c676f114079bc5fefd3b49ecb3a036f49779d7b6d3c6a64f190d0a50'
            'a585ca7c80ec4b05e88622fe98f97c3ee249dbe8a93bca611656d6a07fb36734c27691ebf5578b8adfa71b128466efc7f43513e265feab2b1942e80185555deb'
            '1c03f218678286007b60b041381406187c81f6169e27777312ee92db5537847c975bec4739a01deffbdac7012e5fee764e3636a06fb68870aaa925849ea12568'
            '82a0129dc7dfb103e61ae268572f5e4630fd2469f6a40778290dee0f3db65e50887b91ed9d7db8a6e50d6e162b3ed7ee286492952cd54d2518d344267bddcce7'
            '6ee021f3f9fa207269bee69e06050ff93574e623c4d18521c876e175ad6bd122ff69863d34e89c96d2c07ed59907af3d2dfbcbb7c4c675157cf5a305b5d3e9b6')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/001-wt-4.4.0-fix-find-gm.patch"
  patch -p1 -i "${srcdir}/002-wt-4.4.0-skip-some-examples.patch"
  patch -p1 -i "${srcdir}/003-wt-4.4.0-disable-win32-configdir.patch"
  patch -p1 -i "${srcdir}/004-wt-4.4.0-disable-win32-examples-bat.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DCONNECTOR_HTTP=ON \
    -DWT_WRASTERIMAGE_IMPLEMENTATION=GraphicsMagick \
    -DGM_PREFIX=${MINGW_PREFIX} \
    -DWEBUSER=http \
    -DWEBGROUP=http \
    -DUSE_SYSTEM_SQLITE3=ON \
    -DBUILD_EXAMPLES=OFF \
    -DINSTALL_EXAMPLES=ON \
    -DINSTALL_DOCUMENTATION=ON \
    -DBUILD_TESTS=OFF \
    -DSHARED_LIBS=ON \
    -DBOOST_DYNAMIC=ON \
    -DHARU_DYNAMIC=ON \
    -DMYSQL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/wt/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
